#!/usr/bin/perl
use strict;
use modules::encode;

package csvoiceme;

sub csvoiceme {

    my ($dbh,$remote,$csnum,$input) = @_;
    my $sth; my $auth;
    if ($$input[4] eq "") {
	print $remote "$csnum O $$input[0] :Syntax :VOICEME <#channel>\n";
    }
    else {
	$sth=$dbh->prepare("SELECT auth FROM ninfo WHERE num = '$$input[0]' AND code = '".encode::encode($$input[0])."';");
	$sth->execute;
	$auth=$sth->fetchrow_array;
	if (!$auth) {
	    print $remote "$csnum O $$input[0] :You have to identify yourself with NickServ before using this command.\n";
	}
	else {
	    $sth=$dbh->prepare("SELECT level FROM users WHERE channel = '$$input[4]' AND rnick = '$auth' AND level > 49");
	    $sth->execute;

	    if (!($sth->fetchrow_array)) {
		print $remote "$csnum O $$input[0] :Access denied.\n";
	    }
	    else {
		$sth=$dbh->prepare("SELECT isop FROM onchan WHERE num = '$$input[0]' AND code = '".encode::encode($$input[0])."' AND channel = '$$input[4]' and isvoice = '0';");
		$sth->execute;
		if (!$sth->fetchrow_array) {
		    print $remote "$csnum O $$input[0] :I don't think so.\n";
		}
		else {
		    print $remote "$csnum O $$input[0] :Giving you voice on $$input[4].\n";
		    $sth=$dbh->prepare("UPDATE onchan SET isvoice = '1' WHERE channel = '$$input[4]' AND num = '$$input[0]' AND code = '".encode::encode($$input[0])."';");
		    $sth->execute;
		    print $remote "$csnum M $$input[4] +v $$input[0]\n";
		}
	    }
	}
    }

}
1;
